package org.huoran.sakamichi.study.S_20170608_netty_simpleTCP;


import org.huoran.sakamichi.main.netty.simpleTCP.TCPClient;
import org.huoran.sakamichi.main.netty.simpleTCP.TCPServer;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class NettyTCPStudy {
    private static final String EXIT_FLAG = "exit";
    private String host;
    private int port;

    private TCPServer server;
    private TCPClient client;

    public TCPServer getServer(){
        return this.server;
    }

    public TCPClient getClient(){
        return this.client;
    }

    public NettyTCPStudy(String host, int port){
        this.host = host;
        this.port = port;
        initNettyAndStart();
    }

    private void initNettyAndStart(){
        server = new TCPServer(port);
        server.start();
        client = new TCPClient(host,port);
        client.start();
    }

    public void shutdown(){
        client.shutdown();
        server.shutdown();
    }

    public void sendWithSimpleSocketClient(){
        try{
            Socket socket = new Socket(host, port);
            BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
            PrintWriter os = new PrintWriter(socket.getOutputStream());
            BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            System.out.println("input message content:");
            // block
            String readline = sin.readLine();
            while(!readline.equals(EXIT_FLAG)){
                os.print(readline);
                os.flush();
                // block
                readline = sin.readLine();
            }
            os.close();
            is.close();
            socket.close();
        }catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void sendWithNettyClient(){
        try{
            System.out.println("input message content:");
            BufferedReader sin = new BufferedReader(new InputStreamReader(System.in));
            String readline = sin.readLine();
            while(!readline.equals(EXIT_FLAG)){
                client.send(readline);
                // block
                readline = sin.readLine();
            }
            client.send(EXIT_FLAG);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        // config
        String host = "127.0.0.1";
        int port = 2333;
        NettyTCPStudy study = new NettyTCPStudy(host, port);

        // test with netty (block by inputStream)
        System.out.println("----- netty client begin -----");
        study.sendWithNettyClient();
        System.out.println("----- netty client end -----");

        // test with simple socket (block by inputStream)
        System.out.println("----- socket client begin -----");
        study.sendWithSimpleSocketClient();
        System.out.println("----- socket client end -----");

        // close all
        study.shutdown();
    }
}
